<script setup lang="ts" name="ConsultList">
import { ref } from 'vue'
import ConsultItem from './consult-item.vue'
import type { ConsultOrderItem } from '@/types/consult'
import type { ConsultType } from '@/enums'
import { getConsultOrderList } from '@/api/consult'
const { type } = defineProps<{
  type: ConsultType
}>()
const list = ref<ConsultOrderItem[]>([])
const loading = ref(false)
const finished = ref(false)

const params = {
  current: 1,
  pageSize: 5,
  type
}
const onLoad = async () => {
  const res = await getConsultOrderList(params)
  list.value.push(...res.data.rows)

  // 判断是否还有数据
  if (list.value.length < res.data.total) {
    // 如果有，则页码++ 代表还有下一页数据
    params.current++
  } else {
    // 没有数据啦
    finished.value = true
  }

  // 加载状态结束了
  loading.value = false
}

const del = (id: string) => {
  // 数组中删除元素的几种方式
  /**
   * 1、根据索引删除，先找到索引findIndex
   * 2、数组的filter方法
   */
  list.value = list.value.filter(item => item.id !== id)
}
</script>

<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      @load="onLoad"
      :finished="finished"
      finished-text="没有数据啦~"
    >
      <consult-item v-for="item in list" :key="item.id" :item="item" @del="del" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
